<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('酒店日报表')"/>
    <th:block th:include="include :: layout-latest-css"/>
    <th:block th:include="include :: ztree-css"/>
</head>
<style>
    select {
        padding-top: 3px !important;
    }

    .select-list {
        margin-top: 5px !important;
    }

    .select-list li p {
        width: auto !important;
        margin-top: 7px !important;
    }

    .form-header {
        margin: 10px auto !important;

    }


</style>
<body class="gray-bg">
<div class="ui-layout-center">
    <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="project-form">
                    <div class="col-sm-12">
                        <div class="select-list">
                            <ul>
                                <li>
                                    <p>年份：</p>
                                    <select name="years" class="form-control">
                                        <option value="">请选择</option>
                                        <option value="2020">2020</option>
                                        <option value="2021">2021</option>
                                        <option value="2022">2022</option>
                                        <option value="2023">2023</option>
                                        <option value="2024">2024</option>
                                        <option value="2025">2025</option>
                                    </select>
                                </li>
                                <li>
                                    <p>月份：</p>
                                    <select name="months" class="form-control">
                                        <option value="">请选择</option>
                                        <option value="1">一月</option>
                                        <option value="2">二月</option>
                                        <option value="3">三月</option>
                                        <option value="4">四月</option>
                                        <option value="5">五月</option>
                                        <option value="6">六月</option>
                                        <option value="7">七月</option>
                                        <option value="8">八月</option>
                                        <option value="9">九月</option>
                                        <option value="10">十月</option>
                                        <option value="11">十一月</option>
                                        <option value="12">十二月</option>
                                    </select>
                                </li>
                                <li>
                                    <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                            class="fa fa-search"></i>&nbsp;搜索</a>
                                    <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                            class="fa fa-refresh"></i>&nbsp;重置</a>
                                </li>
                            </ul>
                        </div>
                    </div>
                </form>
            </div>
            <div class="col-sm-12 select-table table-striped">

                <div class="btn-group-sm" id="toolbar" role="group">
                    <a class="btn btn-success" onclick="$.modal.open('新增月数据',prefix_month+'/add?type=hotel')">
                        <i class="fa fa-plus"></i> 新增
                    </a>
                    <a class="btn btn-primary single disabled" onclick="edit();">
                        <i class="fa fa-edit"></i> 修改
                    </a>
                </div>
                <table id="pro_table"></table>
            </div>

        </div>
    </div>
</div>

<th:block th:include="include :: footer"/>
<th:block th:include="include :: layout-latest-js"/>
<th:block th:include="include :: ztree-js"/>
<th:block th:include="include :: bootstrap-table-editable-js"/>

<script th:inline="javascript">
    var prefix = ctx + 'capitalInput';
    $(function () {

        var options = {
            url: prefix + '/selectAllPro',
            uniqueId: "id",
            id: 'pro_table',
            createUrl: prefix + "/add",
            modalName: "酒店日报",
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            pagination: false,
            detailView: true,
            detailFormatter: function (index, row, $detail) {
                initChildTable(index, row, $detail);
            },
            columns: [
                {
                    field: 'id',
                    title: 'id',
                    visible: false
                },
                {
                    field: 'proName',
                    title: '项目名',
                    align: "center"
                },
                {
                    field: 'proPosition',
                    title: '项目位置',
                    align: "center"
                },
                {
                    field: 'proType',
                    title: '项目类型',
                    align: "center"
                },
                {
                    field: 'capitalPerson',
                    title: '劳资负责人',
                    align: "center"
                },
                {
                    title: '操作',
                    align: "left",
                    formatter: function (value, row, index) {
                        return '<a class="btn btn-success btn-xs" onclick="openCapital(\'' + row.id + '\')" href="javascript:void(0)"><i class="fa fa-edit"></i>添加</a> ';
                    }
                }
            ]
        }
        $.table.init(options);
    });

    initChildTable = function (index, row, $detail) {
        var columns = [];
        var other;
        columns.push({field: 'years', title: '年'});
        columns.push({field: 'months', title: '月'});
        columns.push({field: 'days', title: '日'});
        columns.push({
            title: '操作', formatter: function (value, row, index) {
                return '<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="deleteData(\'' + row.project_id + '\',\'' + row.years + '\',\'' + row.months + '\',\'' + row.days + '\')"><i class="fa fa-remove"></i>删除</a>';
            }
        });

        var childTable = $detail.html('<table></table>').find('table');
        $(childTable).bootstrapTable({
            url: prefix + "/selectDataDate",
            queryParams: {"proId": row.id},
            method: 'post',
            uniqueId: row.id,
            sidePagination: "server",
            contentType: "application/x-www-form-urlencoded",
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            pagination: false,
            detailView: true,
            detailFormatter: function (index, row, $detail) {
                asdasd(index, row, $detail);
            },
            columns: columns,
        });
    }

    asdasd = function (index, row, $detail) {
        var columns = [];
        columns.push({field: '工种', title: '工种'});

        $.ajax({
            cache: true,
            type: "POST",
            url: prefix + "/selectPositionByProjectId",
            data: {
                "projectId": row.project_id
            },
            async: false,
            success: function (data) {
                console.log(data);
                for (var i = 0; i < data.length; i++) {
                    columns.push({field: data[i].position_name, title: data[i].position_name, editable: true});
                }
                var childTable = $detail.html('<table></table>').find('table');
                $(childTable).bootstrapTable({
                    url: prefix + "/selectData",
                    queryParams: {
                        "project_id": row.project_id,
                        "years": row.years,
                        "months": row.months,
                        "days": row.days
                    },
                    method: 'post',
                    uniqueId: row.id,
                    sidePagination: "server",
                    contentType: "application/x-www-form-urlencoded",
                    onEditableSave: onEditableSave,
                    detailFormatter: function (index, row, $detail) {
                        asdasd(index, row, $detail);
                    },
                    columns: columns,
                });
            },
            error: function (request) {
            }
        });
    }

    function onEditableSave(field, row, oldValue, $el) {
        var years = row.years;
        var months = row.months;
        var days = row.days;
        var work_id = row.work_id;
        var position = field;
        var value = row[field];

        $.ajax({
            cache: true,
            type: "POST",
            url: prefix + "/updateData",
            data: {
                "years": years,
                "months": months,
                "days": days,
                "work_id": work_id,
                "position": position,
                "value": value,
            },
            async: false,
            success: function (data) {

            },
            error: function (request) {
            }
        });

        //alert("字段名：" + field + "，当前值：" + row[field]  + "，旧值：" + oldValue);
    }

    function edit() {
        months_ID = $('#bootstrap-table').bootstrapTable('getSelections')[0].id;
        $.modal.open('修改月数据', prefix_month + '/edit/' + months_ID + "?type=hotel")
    }

    function openCapital(id) {
        $.modal.open('新增劳资数据', prefix + '/add?id=' + id);
    }

    function deleteData(project_id, years, months, days) {

        $.modal.confirm("确实删除该条数据吗?", function() {
            $.ajax({
                cache: true,
                type: "POST",
                url: prefix + "/deleteData",
                data: {
                    "project_id": project_id,
                    "years": years,
                    "months": months,
                    "days": days
                },
                async: false,
                success: function (data) {
                    if(data.code == 0){
                        $.table.search();
                    }
                },
                error: function (request) {
                }
            });
        });
    }
</script>
</body>
</html>